#!/bin/bash

#remove obs-studio package first
if package_installed obs-studio ;then
  echo "Removing obs-studio package first..."
  apt_lock_wait
  sudo apt purge -y --autoremove obs-studio
fi

#remove obs package first
if package_installed obs ;then
  echo "Removing obs package first..."
  apt_lock_wait
  sudo apt purge -y --autoremove obs
fi

#remove libobs0 package first
if package_installed libobs0 ;then
  echo "Removing libobs0 package first..."
  apt_lock_wait
  sudo apt purge -y --autoremove libobs0
fi

#remove libobs0 package first
if package_installed libobs0t64 ;then
  echo "Removing libobs0t64 package first..."
  apt_lock_wait
  sudo apt purge -y --autoremove libobs0t64
fi

#remove libobs-dev package first
if package_installed libobs-dev ;then
  echo "Removing libobs-dev package first..."
  apt_lock_wait
  sudo apt purge -y --autoremove libobs-dev
fi

if [ "$__os_codename" == "bullseye" ]; then
  if ! package_available libqt6core6 ; then
    #Checking if using armv6
    if [ ! -z "$(cat /proc/cpuinfo | grep ARMv6)" ];then
      error "armv6 cpu not supported"
    fi
    # add debian keyring
    rm -f /tmp/debian-archive-keyring.deb
    wget -qO /tmp/debian-archive-keyring.deb http://ftp.us.debian.org/debian/pool/main/d/debian-archive-keyring/debian-archive-keyring_2021.1.1+deb11u1_all.deb
    apt_lock_wait
    sudo apt install -y /tmp/debian-archive-keyring.deb || error "Failed to install debian-archive-keyring"
    rm -f /tmp/debian-archive-keyring.deb
    # backports are automatically only used when required https://backports.debian.org/Instructions/
    echo 'deb http://deb.debian.org/debian bullseye-backports main contrib non-free' | sudo tee /etc/apt/sources.list.d/bullseye-backports.list
    apt_update
  fi
  #install bullseye version
  install_packages qt6-qpa-plugins https://github.com/Pi-Apps-Coders/files/releases/download/large-files/obs-studio-29.1.3-1-armhf-bullseye.deb || exit 1
elif [ "$__os_codename" == "bookworm" ]; then
  # install bookworm version
  install_packages qt6-qpa-plugins https://github.com/Pi-Apps-Coders/files/releases/download/large-files/obs-studio-30.2.2-1-armhf-bookworm.deb || exit 1
else
  # note: bump this version to whatever the version of OBS-Studio we have precompiled debs for
  # if the distro version is equal or newer, prefer it to our debs
  # perform the package_is_new_enough check after purging obs-studio to prevent accidentally checking our own precompiled deb if it happens to still be installed 
  if package_is_new_enough obs-studio 30.2.2 ;then
    install_packages obs-studio || exit 1
  else
    error "User error: You are not using a supported Pi-Apps distribution and OBS Studio from your distribution was too old or not available."
  fi
fi

# obtain model
get_model &>/dev/null
if [[ "$model" == *"Raspberry Pi"* ]]; then
  #Create workaround wrapper script to override MESA_GL_VERSION
  file -bL --mime /usr/bin/obs | grep -q "binary" && sudo mv /usr/bin/obs /usr/bin/obs-exec
  echo '#!/bin/bash
LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/libGL.so MESA_GL_VERSION_OVERRIDE=3.3 /usr/bin/obs-exec "$@"' | sudo tee /usr/bin/obs >/dev/null
  sudo chmod +x /usr/bin/obs
fi
exit 0
